﻿/************************************************************************

	File:			pak_io.c
	Version:		1.0
	Changes:		A
	Author:			Stanley Lau
	Date:			2010.07.26
	Purpose:		File IO Cross-platform Module Functions Implements

************************************************************************/

#include "pak_io.h"
#include <debugheader.h>

/*
FILE* _OpenFile( const char* pstrFileName, const char* pstrMode )
{
	int		bResult = 0;
	FILE*	pFile = NULL;

	PROCESS_ERROR( pstrFileName && pstrMode );
	PROCESS_ERROR( pFile = fopen( pstrFileName, pstrMode ) );

	bResult = 1;
Exit0:
	if( 0==bResult )
	{
		if( pFile ) fclose( pFile );
		pFile = NULL;
	}
	return pFile;
}
*/

int _ReadFile( FILE* hFile, char* pstrReadBuffer, size_t nReadLen, size_t* pnReadLen )
{
	int		bResult = 0;
	size_t	nReaded = 0;

	PROCESS_ERROR( hFile && pstrReadBuffer  && pnReadLen );
	while( nReaded < nReadLen )
	{
		size_t nOneReaded = 0;
		nOneReaded = fread( pstrReadBuffer + nReaded, 1, nReadLen, hFile );
		if( 0==nOneReaded )
			break;
		nReaded += nOneReaded;
	}

	*pnReadLen = nReaded;

	bResult = 1;
Exit0:
	return bResult;
}

int _WriteFile( FILE* hFile, char* pstrWriteBuffer, size_t nWriteLen, size_t* pnWriteLen )
{
	int		bResult = 0;
	size_t	nWrited = 0;

	PROCESS_ERROR( hFile && pstrWriteBuffer && pnWriteLen );
	while( nWrited < nWriteLen )
	{
		size_t nOneWrited = 0;
		nOneWrited = fwrite( pstrWriteBuffer + nWrited, 1, nWriteLen, hFile );
		PROCESS_ERROR( nOneWrited );
		nWrited += nOneWrited;
	}

	*pnWriteLen = nWrited;
	bResult = 1;
Exit0:
	return bResult;
}

int _ReadFileAt( FILE* hFile, char* pstrReadBuffer, size_t nReadBufferLen, size_t* pnReadLen, long lPos )
{
	int	bResult = 0;

	PROCESS_ERROR( hFile && pstrReadBuffer  && pnReadLen );
	PROCESS_ERROR( _SetFilePos( hFile, lPos, SEEK_SET ) );
	PROCESS_ERROR( bResult = _ReadFile( hFile, pstrReadBuffer, nReadBufferLen, pnReadLen ) );

Exit0:
	return bResult;
}

int _WriteFileAt( FILE* hFile, char* pstrWriteBuffer, size_t nWriteLen, size_t* pnWriteLen, long lPos )
{
	int bResult = 0;

	PROCESS_ERROR( hFile && pstrWriteBuffer && pnWriteLen );
	PROCESS_ERROR( _SetFilePos( hFile, lPos, SEEK_SET ) );
	PROCESS_ERROR( bResult = _WriteFile( hFile, pstrWriteBuffer, nWriteLen, pnWriteLen ) );

Exit0:
	return bResult;
}
int _SetFilePos( FILE* hFile, long lFilePos, int iBegin )
{
	int	bResult = 0;

	PROCESS_ERROR( hFile );
	PROCESS_ERROR( 0==fseek( hFile, lFilePos, iBegin ) );
	
	bResult = 1;
Exit0:
	return bResult;
}

int _GetFilePos( FILE* hFile, long* plFilePos )
{
	int		bResult = 0;
	long	lPos = 0;

	PROCESS_ERROR( hFile && plFilePos );
	lPos = ftell( hFile );
	PROCESS_ERROR( lPos >= 0 );

	*plFilePos = lPos;
	bResult = 1;
Exit0:
	return bResult;
}

int _GetFileSize( FILE* hFile, long* plSize )
{
	int		bResult = 0;
	long	lOldPos = 0, lNewPos = 0;

	PROCESS_ERROR( hFile && plSize );
	PROCESS_ERROR( _GetFilePos( hFile, &lOldPos ) );
	PROCESS_ERROR( _SetFilePos( hFile, 0, SEEK_END ) );
	PROCESS_ERROR( _GetFilePos( hFile, &lNewPos ) );
	PROCESS_ERROR( _SetFilePos( hFile, lOldPos, SEEK_SET ) );

	*plSize = lNewPos;
	bResult = 1;
Exit0:
	return bResult;
}

int _CloseFile( FILE* hFile )
{
	int	bResult = 0;

	PROCESS_ERROR( hFile );
	PROCESS_ERROR( 0==fclose( hFile ) );
	bResult = 1;
Exit0:
	return bResult;
}
//Last line for linux

